package com.bawei.home.view

import androidx.fragment.app.activityViewModels
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.bawei.aspect.CheckLoginState
import com.bawei.aspect.annotation.CheckLogin
import com.bawei.base.RoutePath
import com.bawei.common.mvi.BaseMviFragment
import com.bawei.home.R
import com.bawei.home.adapter.HomeViewPagerAdapter
import com.bawei.home.databinding.FragmentHomeBinding
import com.bawei.home.entity.SimpleTypeEntity
import com.bawei.home.intent.HomeViewIntent
import com.bawei.home.uistate.HomeUiState
import com.bawei.home.viewmodel.HomeViewModel
import com.google.android.material.tabs.TabLayoutMediator
import dagger.hilt.android.AndroidEntryPoint


@AndroidEntryPoint
@Route(path = RoutePath.PATH_FRAGMENT_HOME)
class HomeFragment : BaseMviFragment<FragmentHomeBinding>() {

    private val mViewModel: HomeViewModel by activityViewModels()
    private val fragments = mutableListOf<HomeListFragment>()
    private lateinit var homeVpAdapter: HomeViewPagerAdapter
    private var tabList = mutableListOf<SimpleTypeEntity>()

    private val searchHotWords = listOf("热门视频", "今日头条视频", "许家印头条", "抖音视频", "快手视频", "西瓜视频")


    override fun getLayoutId(): Int = R.layout.fragment_home

    override fun initViews() {
        //设置搜索框推荐的热词
        mDataBinding.tvbSearch.setDatas(searchHotWords)
        //消息按钮点击事件
        mDataBinding.ivMsg.setOnClickListener {
            jumpToMsgPage()
        }
        //分类按钮点击事件
        mDataBinding.ivTypeMenu.setOnClickListener {
            showAllTypes()
        }

        homeVpAdapter = HomeViewPagerAdapter(fragments, this@HomeFragment.childFragmentManager, lifecycle)
        mDataBinding.vp2Home.offscreenPageLimit = 1
        mDataBinding.vp2Home.adapter = homeVpAdapter

        //关联TabLayout与ViewPager2
        TabLayoutMediator(mDataBinding.tabTypeLayout, mDataBinding.vp2Home){tab, position ->
            tab.text = tabList[position].typename
        }.attach()

    }

    override suspend fun sendViewIntent() {
        mViewModel.channel.send(HomeViewIntent.LoadVideoType)
    }


    override suspend fun collectUiState() {
        mViewModel.uiState.collect{
            when(it){
                is HomeUiState.LoadEmpty -> {}
                is HomeUiState.LoadSimpleTypeSuccess -> initTabFragments(it.list)
                is HomeUiState.LoadSimpleTypeFail -> showToastMsg(it.msg)
                else -> {}
            }
        }
    }

    private fun initTabFragments(list: List<SimpleTypeEntity>) {
        if(list.isNotEmpty()){
            //添加推荐Tab实体
            tabList.add(SimpleTypeEntity("",0, -1, 0,"","推荐"))
            fragments.add(ARouter.getInstance().build(RoutePath.PATH_FRAGMENT_HOME_LIST).withInt("typeId", -1).withString("channelId", "").navigation() as HomeListFragment)
            //添加接口返回Tab数据
            list.forEach {
                tabList.add(it)
                fragments.add(ARouter.getInstance().build(RoutePath.PATH_FRAGMENT_HOME_LIST).withInt("typeId", it.id).withString("channelId", it.channelid).navigation() as HomeListFragment)
            }
            //设置Tab标签
            tabList.forEach {
                mDataBinding.tabTypeLayout.addTab(mDataBinding.tabTypeLayout.newTab().setText(it.typename))
            }
            homeVpAdapter.notifyDataSetChanged()
        }
    }


    private fun showAllTypes() {
        showToastMsg("视频类型")
    }

    @CheckLogin
    fun jumpToMsgPage() {
        showToastMsg("消息页面")
    }

    override fun onResume() {
        super.onResume()
        mDataBinding.tvbSearch.startViewAnimator()
    }

    override fun onStop() {
        super.onStop()
        mDataBinding.tvbSearch.stopViewAnimator()
    }


}